package com.ruoyi.common.utils.wxlogin;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.codec.binary.Base64;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.protocol.HTTP;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

public class WeChatReCode {
	
	
	
	public static String access_token() {
		String token="";
		String requestUrl="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wxe66b24b0cc716cc7&secret=ea6591b1d8de16338a184609ab7df66e";
		// 发送post请求读取调用微信 https://api.weixin.qq.com/sns/jscode2session 接口获取openid用户唯一标识
//		requestUrl.replace("APPID", "wxbc63b414a92a6fc3");
//		requestUrl.replace("APPSECRET", "bfb195a9f7e5825b6a0496c3fb21d2ae");
		token=NetUtil.get(requestUrl);
		return token;
	}
	
	public static JSONObject getCode() {
		String requestUrl="https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token="+JSON.parseObject(access_token()).getString("access_token");
		Map<String, Object> requestUrlParam = new HashMap<String, Object>();
		//requestUrlParam.put("access_token", "extensionCode=woY2oVGG&companyId=1"); 
		requestUrlParam.put("scene", "extensionCode"); // 开发者设置中的appSecret
		requestUrlParam.put("page", "pages/index/index"); // 必须是已经发布的小程序存在的页面（
		//requestUrlParam.put("width",430); // 二维码的宽度
		//requestUrlParam.put("autoColor",false); // 自动配置线条颜色
		//requestUrlParam.put("lineColor","{r:0,g:0,b:0}"); // 自动配置线条颜色
		//requestUrlParam.put("isHyaline",false); // 是否需要透明底色，
		// 发送post请求读取调用微信 https://api.weixin.qq.com/sns/jscode2session 接口获取openid用户唯一标识
		JSONObject jsonObject = JSON.parseObject(NetUtil.sendPost(requestUrl, requestUrlParam));
		return jsonObject;
	}
	public static String MyGetAccessToken(){
		try {
			String access_token = JSONObject.parseObject(access_token()).getString("access_token");
			if(access_token!=null) {
				Map<String, Object> params = new HashMap<>();
				params.put("scene", "extensionCode=sdfsdf");
				params.put("page", "pages/index/index");
				params.put("width", 430);
				CloseableHttpClient  httpClient = HttpClientBuilder.create().build();
				HttpPost httpPost = new HttpPost("https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token="+access_token);
				httpPost.addHeader(HTTP.CONTENT_TYPE, "application/json");
				String body = JSON.toJSONString(params);
				StringEntity entity;
				entity = new StringEntity(body);
				entity.setContentType("image/png");
				httpPost.setEntity(entity);
				HttpResponse response;
				response = httpClient.execute(httpPost); InputStream inputStream = response.getEntity().getContent();
				ByteArrayOutputStream swapStream = new ByteArrayOutputStream();
				byte[] data = null;
				byte[] buffer = new byte[8192];
				int bytesRead = 0;
				while((bytesRead = inputStream.read(buffer, 0, 8192)) != -1) {
					swapStream.write(buffer, 0, bytesRead);
				}
				data = swapStream.toByteArray();
				swapStream.flush();
				swapStream.close();
				return new String(Base64.encodeBase64(data));
			} else {
				System.out.println("获取access_token错误");
			}
		}catch (Exception e){

		}
			return "";
	}

	public static void main(String[] args) throws IOException {
        String access_token = JSONObject.parseObject(access_token()).getString("access_token");
        if(access_token!=null) {
            Map<String, Object> params = new HashMap<>();
            params.put("scene", "extensionCode=sdfsdf");
            params.put("page", "pages/index/index");
            params.put("width", 430);
            CloseableHttpClient  httpClient = HttpClientBuilder.create().build();
            HttpPost httpPost = new HttpPost("https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token="+access_token);
            httpPost.addHeader(HTTP.CONTENT_TYPE, "application/json");
            String body = JSON.toJSONString(params);
            StringEntity entity;
            entity = new StringEntity(body);
            entity.setContentType("image/png");
            httpPost.setEntity(entity);
            HttpResponse response;
            response = httpClient.execute(httpPost); InputStream inputStream = response.getEntity().getContent();
            ByteArrayOutputStream swapStream = new ByteArrayOutputStream();
            byte[] data = null;
            byte[] buffer = new byte[8192];
            int bytesRead = 0;
            while((bytesRead = inputStream.read(buffer, 0, 8192)) != -1) {
            	swapStream.write(buffer, 0, bytesRead);
            }
            data = swapStream.toByteArray();
            swapStream.flush();
            swapStream.close();
            System.out.println(new String(Base64.encodeBase64(data)));
        } else {
            System.out.println("获取access_token错误");
        }
	}

}
